#!/usr/bin/python
#coding:utf8
from zabbix import Worker


worker = Worker('http://192.168.120.221/zabbix')

(options,parser) = Worker.getCLIOpts()

###添加操作######
if options.action == 'add':
   if options.object == 'host':
       if options.file:
           worker.importHosts({'file':options.file,'ifcfg':options.ifcfg})
       else:
          if not (options.name and options.group and options.template and options.ipaddr):
             parser.error('hostname groups templates ipaddr are required!') 
          else:
             G = (options.group)[0].split(',')
             T = (options.template)[0].split(',')
             #worker.createHost(options.name,groups=G,interfaces=dict(options.ifcfg,ip=options.ipaddr),templates=T)
             worker.createHost({'host':options.name,'groups':G,'interfaces':dict(options.ifcfg,ip=options.ipaddr),'templates':T})
   elif options.object == 'group':
      if not options.name:
         parser.error('group name is required!')
      else:
         worker.createGroup({'name':options.name})
   elif options.object == 'temp':
      if not (options.name and options.group and options.host):
         parser.error('templatename and group and hosts  are required!')
      else:
         G = (options.group)[0].split(',')
         H = (options.host)[0].split(',')
         worker.createTemplate({'host':options.name,'groups':G,'hosts':H})
   elif options.object == 'item':
      if options.file:
          worker.importItems({'file':options.file})
      else:
          if not (options.name and options.host and options.ikey):
             parser.error('item name,host must be specify and only one host allowed,item key expression required!')
          else:
             #如果需要添加额外的参数，请参考zabbix API,默认是60s
             worker.createItems({'name':options.name,'key':options.ikey,'host':options.host}) 
   elif options.object == 'trigger':
      if options.file:
          worker.importTrigger({'file':options.file})
      else:
          if not (options.name and options.texp and options.tlevel):
             parser.error('trigger name and expression,trigger level are required!')
          else:
             #如果需要添加额外的参数，请参考zabbix API
             worker.createTrigger({'desc':options.name,'expression':options.texp,'level':options.tlevel})    

##查询操作###
elif options.action == 'query':
   if options.object ==  'host':
      pass
###更新操作###  
elif options.action == 'update':
   if options.object == 'host':
      pass
###删除操作##
elif options.action == 'delete':
   if options.object == 'host':
      pass
